package com.mmche.web.dao;

import java.util.List;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import com.mmche.sell.pojo.SellOrder;
import com.mmche.sell.dao.SellOrderDao;
import com.mmche.sell.dao.impl.SellOrderDaoImpl;

@Repository("webSellOrderDao")
public class WebSellOrderDao extends SellOrderDaoImpl implements
		SellOrderDao {

	@Override
	public List<SellOrder> getSaledPage(int limit) {
		Query query = new Query(Criteria.where("status").is(SellOrder.STATUS_SALED));
		query.addCriteria(Criteria.where("onlineOrder").ne(null));
		query.fields().exclude("client");
		query.fields().exclude("brand");
		query.fields().exclude("models");
		query.fields().exclude("makeMan");
		query.fields().exclude("updateMan");
		query.fields().exclude("onlineOrder");
		query.fields().exclude("comment.updateMan");
		query.fields().exclude("comment.checkMan");
		query.fields().exclude("car");
		query.limit(limit);
		query.sort().on("saledTime", Order.DESCENDING);
		try{
			return this.getMongoTemplate().find(query, SellOrder.class);
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public int getSaledCount() {
		Query query = new Query(Criteria.where("status").is(SellOrder.STATUS_SALED));
		query.addCriteria(Criteria.where("onlineOrder").ne(null));
		query.sort().on("saledTime", Order.DESCENDING);
		Long k = this.getMongoTemplate().count(query, SellOrder.class);
		return k.intValue();
	}

	@Override
	public List<SellOrder> getSaledPage(int start, int limit) {
		Query query = new Query(Criteria.where("status").is(SellOrder.STATUS_SALED));
		query.addCriteria(Criteria.where("onlineOrder").ne(null));
		query.sort().on("saledTime", Order.DESCENDING);
		query.fields().exclude("client");
		query.fields().exclude("brand");
		query.fields().exclude("models");
		query.fields().exclude("makeMan");
		query.fields().exclude("updateMan");
		query.fields().exclude("onlineOrder");
		query.fields().exclude("comment.updateMan");
		query.fields().exclude("comment.checkMan");
		query.fields().exclude("car");
		query.skip(start).limit(limit);
		return this.getMongoTemplate().find(query, SellOrder.class);
	}
}
